package com.ericom.webapp.action;

import java.util.List;

import com.ericom.model.ContentCategory;
import com.ericom.service.GenericManager;

public class ContentCategoryAction extends BaseAction {

	private GenericManager<ContentCategory, Long> contentCategoryManager;
	private List contentCategorys;
	private ContentCategory contentCategory;
	private Long id;

	public ContentCategory getContentCategory() {
		return contentCategory;
	}

	public void setContentCategory(ContentCategory contentCategory) {
		this.contentCategory = contentCategory;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setContentCategoryManager(
			GenericManager<ContentCategory, Long> contentCategoryManager) {
		this.contentCategoryManager = contentCategoryManager;
	}

	public String list() {
		contentCategorys = contentCategoryManager.getAll();
		return SUCCESS;
	}

	public List getContentCategorys() {
		return contentCategorys;
	}

	public String delete() {
		contentCategoryManager.remove(contentCategory.getId());
		this.saveMessage(getText("contentCategory.deleted"));
		return this.SUCCESS;
	}

	public String edit() {
		if (id != null) {
			contentCategory = contentCategoryManager.get(id);
		} else {
			contentCategory = new ContentCategory();
		}
		return this.SUCCESS;
	}

	public String save() throws Exception {
		if (this.cancel != null) {
			return this.CANCEL;
		}
		if (this.delete != null) {
			return this.delete();
		}

		boolean isNew = (contentCategory.getId() == null);
		contentCategory = contentCategoryManager.save(contentCategory);

		String key = (isNew) ? "contentCategory.added"
				: "contentCategory.updated";
		this.saveMessage(getText(key));

		if (!isNew) {
			return this.INPUT;
		} else {
			return this.SUCCESS;
		}
	}
}
